$(document).ready(function () {
    layui.use(['element', 'layer'], function () {
        var element = layui.element;
        var layer = layui.layer;

        initServerInfo(layer);
        // 检查服务器软件版本
        setTimeout(checkServerVersion(layer), 5000);

    });
});

function initServerInfo(layer) {

    // layer.msg(apiHostUrl);

    $.ajax({
        url: apiHostUrl + "admin/server/getServerInfo",
        headers: {'Authorization': token},
        contentType: "application/json",
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.errorCode.toString() === "0") {
                // 获取成功
                var data = result.returnData;
                $('#main-server-name').html(data.serverName);
                $('#main-server-type').html(data.serverType);
                $('#main-server-engine').html(data.serverEngine);
                $('#main-server-encording').html(data.serverEncording);
                $('#main-server-port').html(data.serverWebPort);
                $('#main-server-jdk').html("" + data.serverJDKVersion);

                var realVersion = $.base64.atob(softVersion);
                var realVersionId = $.base64.atob(softVersionId);

                $('#main-server-version').html("WMS管理平台&nbsp;v" + realVersion + "/版本号" + realVersionId + "<a href=\"http://www.junxtec.com\" style=\"color:#FF5722;\" target=\"_blank\"><b>&nbsp;&nbsp;查看最新版本</a>");
            }
            else if (result.errorCode === "401") {
                layer.alert(result.errorCode + ":" + result.errorMsg,
                    {
                        title: "提示",
                        btn: ['确定']
                    }, function () {
                        window.location = currentHostUrl;
                    });
            }
            else {
                layer.msg(result.errorCode + ":" + result.errorMsg);
            }
        },
        error: function (result) {
            if (result.errorCode == null || result.errorMsg == null) {
                layer.msg("网络连接失败");
                window.location = currentHostUrl;
            }
            else {
                layer.msg("获取服务器信息失败");
            }
        }
    });

}

function checkServerVersion(layer) {

    $.ajax({
        url: apiHostUrl + "admin/server/getServerVersion",
        headers: {'Authorization': token},
        contentType: "application/json",
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.errorCode.toString() === "0") {
                // 获取成功
                var data = result.returnData;

                // 处理服务器软件版本更新
                var realSoftVersionId = $.base64.atob(softVersionId);

                if (data.serverVersionId > realSoftVersionId) {
                    layer.open({
                        title: "发现新版本",
                        type: 0,
                        content: "版本：&nbsp;&nbsp;&nbsp;v" + data.serverVersion + "<br>版本号：" + data.serverVersionId
                    });
                }
                else {
                    // 当前版本已最新
                }

            }
            else if (result.errorCode === "401") {
                layer.alert(result.errorCode + ":" + result.errorMsg,
                    {
                        title: "提示",
                        btn: ['确定']
                    }, function () {
                        window.location = currentHostUrl;
                    });
            }
            else {
                layer.msg(result.errorCode + ":" + result.errorMsg);
            }
        },
        error: function (result) {
            if (result.errorCode == null || result.errorMsg == null) {
                layer.msg("网络连接失败");
                window.location = currentHostUrl;
            }
            else {
                layer.msg("获取最新软件版本失败");
            }
        }
    });

}